#!/bin/sh

run_validator()
{
    mono --debug xcsv.exe $1 $2 $3;

    if [ $? != 0 ]; then
        exit 1;
    fi;
}

for dir in `find . -maxdepth 1 -type d | grep -v "\.$" | grep -v "\./\." | sed "s/^\.\///"`; do
    build_done="false"

    for file in `git diff --cached --name-only | grep "^$dir\/"`; do
        if ! [ -d $dir ]; then
            break;
        fi;

        if [ -f $dir/Makefile ]; then
            break;
        fi;

        if [ $file == $dir/README.md ]; then
            run_validator markdown $dir;
        elif [ $file == $dir/Metadata.xml ]; then
            run_validator metadata $dir;
        elif [ `echo $file | grep "^$dir\/Screenshots\/"` ]; then
            run_validator screenshot $dir;
        else
            if [ $build_done == "false" ]; then
                run_validator build $dir mdtool-build;
                build_done="true"
            fi;
        fi;
    done;
done;
